home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3508 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: airdmhor.gen.nz!not-for-mail
  2. From: gumboot@airdmhor.gen.nz (Simon Hosie)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: division problem
  5. Date: 30 Jan 1996 05:14:50 +1300
  6. Organization: Airdmhor
  7. Message-ID: <4eirpq$8ut@airdmhor.gen.nz>
  8. References: <31097D77.11AA@rain.org>
  9. NNTP-Posting-Host: localhost.gen.nz
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. tpaul:
  13. > Can anyone show me why this does not work?  I am a beginner.
  14.  
  15.  1> #include <stdio.h>
  16.  
  17.  3> main ()
  18.  4> {
  19.  5>     int fahrenheit, celsius;
  20.  
  21.  7>     printf("Fahrenheit temperature =?";
  22.  8>     scanf("%d",fahrenheit);
  23.  9>     celsius = 5/9*(fahrenheit-32);
  24. 10>     printf("Fahrenheit = %d, Celsius = %d", fahrenheit, celsius;
  25. 11> }
  26.  
  27.   I don't know if the guy I told off answered this (I just looked at his
  28. answer and got upset).  Firstly you don't have any closing brackets on your
  29. printf()'s, secondly..
  30.  
  31.   I can't quite remember how C chooses the types to use, I think it's the
  32. first thing after the '='..  anyway, it's doing integer math.  The first
  33. thing it does is divide 5 by 9, that gives 0 (the remainder is thrown away)
  34. then it multiplies (fahrenheight - 32) by 0.
  35.  
  36.   The solution is to do your multiplies first.  The solution is _not_ to use
  37. floating point.
  38.